package at.fhooe.stefanirndorfer.quadtree;

import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2d;

import java.util.ArrayList;
import java.util.Random;

import at.fhooe.stefanirndorfer.entity.Entity;

public class QuadTreeNode extends Entity {

	private int size;
	private double[] center;
	private ArrayList<QuadTreeNode> childNodes;
	private float[] color;
	private boolean isLeaf;

	
	public QuadTreeNode(int size, double[] center) {
		super(size, size, center[0]-size/2, center[1]+size/2);
		this.size = size;
		this.center = center;
		childNodes = new ArrayList<QuadTreeNode>();
		isLeaf = true;
		color = randomizeColor();
	}
	
	private float[] randomizeColor() {
		this.color = new float[3];
		Random rand = new Random();
		for (int i = 0; i < color.length; i++) {
			color[i] = rand.nextFloat();
		}
		return color;
	}
	
	//////////////////////////////////////////////////
	// Getters and Setters
	//////////////////////////////////////////////////



	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public double[] getCenter() {
		return center;
	}

	public void setCenter(double[] center) {
		this.center = center;
	}

	public ArrayList<QuadTreeNode> getChildNodes() {
		return childNodes;
	}

	public void setChildNodes(ArrayList<QuadTreeNode> childNodes) {
		this.childNodes = childNodes;
	}
	
	public void setIsLeaf(boolean b) {
		this.isLeaf = b;
	}
	
	public boolean getIsLeaf(){
		return isLeaf;
	}

	
	

	//////////////////////////////////////////////////
	// Overridden Methods
	//////////////////////////////////////////////////
	
	@Override
	public void update(int delta) {
		
	}


	@Override
	public double[] getPosition() {
		
		return null;
	}


	@Override
	public void render() {
		if(isLeaf){
			//Log.info(this, "PosX: " + posX + ", PosY: " + posY + ", Size: " + width);
			
			glColor3f(color[0], color[1], color[2]);
			
			glBegin(GL_QUADS);
			glVertex2d(posX, posY);
			glVertex2d(posX, posY - height);
			glVertex2d(posX + width, posY - height);
			glVertex2d(posX + width, posY);
			glEnd();
			
		} else{
			
//			glColor3f(1.0f, 0.0f, 0.0f);
//			glBegin(GL_LINE_LOOP);
//			//glBegin(GL_QUADS);
//			glVertex2d(posX, posY);
//			glVertex2d(posX, posY - height);
//			glVertex2d(posX + width, posY - height);
//			glVertex2d(posX + width, posY);
//			glEnd();
//			 
		}
	}

	


	
}
